/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * subasta.java
 *
 * Created on 12/05/2011, 12:22:08 AM
 */
package logica;

import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Jorge Maldonado
 */
public class subasta extends javax.swing.JDialog implements Runnable {

    /** Creates new form subasta */
    private ArrayList<usuario> jugadores;
    private int tiempo;
    private float mejor_oferta;
    private propiedad prop;
    private usuario mejor_postor;
    private Thread hilo;

    public subasta(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public subasta(ArrayList<usuario> jugadores, propiedad prop) {
        this(null, true);
        hilo=new Thread(this);
        tiempo = 0;
        mejor_oferta = prop.getPrecio();
        mejor_postor = null;
        this.prop = prop;
        this.jugadores = jugadores;
        for (int i = 0; i < jugadores.size(); i++) {
            jComboBox1.addItem(jugadores.get(i).getNombre());
        }
        this.txtPropuesta.setText(Float.toString(mejor_oferta));
        hilo.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        txtCantidad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPropuesta = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtTiempo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Propiedad");

        jLabel2.setText("Jugador:");

        jLabel3.setText("Cantidad");

        jLabel4.setText("¿Quién da más?");

        jLabel5.setText("Mejor Propuestas:");

        txtPropuesta.setEditable(false);

        jLabel6.setText("Tiempo límite:");

        txtTiempo.setEditable(false);

        jButton1.setText("Oferta");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(10, 10, 10)
                        .addComponent(jComboBox1, 0, 204, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtTiempo)
                            .addComponent(txtPropuesta, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(jButton1))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtPropuesta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtTiempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        usuario aux = null;
        for (int i = 0; i < jugadores.size(); i++) {
            aux = jugadores.get(i);
            if (jComboBox1.getSelectedItem().toString().equals(aux.getNombre())) {
                break;
            }
        }
        float cantidad = Float.parseFloat(txtCantidad.getText());
        if (aux.getDinero() < cantidad) {
            JOptionPane.showMessageDialog(null, "No puede ofrecer tal cantidad");
        } else if (cantidad > mejor_oferta) {
            tiempo = 0;
            mejor_oferta = cantidad;
            mejor_postor = aux;
            txtPropuesta.setText(Float.toString(mejor_oferta));
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtPropuesta;
    private javax.swing.JTextField txtTiempo;
    // End of variables declaration//GEN-END:variables

    public void run() {
        while (tiempo < 10) {
            try {
                txtTiempo.setText(Integer.toString(10 - tiempo));
                tiempo++;
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
        }
        if (mejor_postor != null) {
            mejor_postor.setDinero(mejor_postor.getDinero() - mejor_oferta);
            mejor_postor.getPropiedades().add(prop);
            prop.adquirir(mejor_postor);
            JOptionPane.showMessageDialog(null, "Vendido a " + mejor_postor.getNombre(), "Atención", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "Propiedad no vendida", "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
        dispose();
    }
}
